package com.bl.andr.utillib;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bl.andr.utillib.listener.PageListener;


/**
 * Desc:
 * Created by Ling
 * Date  2017-4-3
 */
public class ViewUtil {


// *************************************************************************
//  自定义UI页面（标题栏 + 内容）
// *************************************************************************

    public static View createActionBar(Activity activity, PageListener listener) {
        // 页面容器
        LinearLayout root = new LinearLayout(activity);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setClickable(true);

        if (listener == null) return root;
        LinearLayout.LayoutParams layoutParams;

        //添加actionbar
        int actionBarResID = listener.getToolbarLayout();
        int actionBarH = listener.getToolBarH();
        if (actionBarResID > 0 && actionBarH > 0) {
            View actionBar = activity.getLayoutInflater().inflate(actionBarResID, null);
            layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, actionBarH);
            root.addView(actionBar, layoutParams);
        }

        // 添加内容布局
        int contentResID = listener.getContentLayout();
        if (contentResID > 0) {
            View content = activity.getLayoutInflater().inflate(contentResID, null);
            layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            root.addView(content, layoutParams);
        }

        return root;
    }


//    @SuppressLint("InflateParams")
//    public static View createActionBar(Activity activity, final int layoutResID) {
//        RelativeLayout relativeLayout = new RelativeLayout(activity);
//        RelativeLayout.LayoutParams rlp;
//        relativeLayout.setClickable(true);
//
//        //添加头部底部背景
//        View iv = activity.getLayoutInflater().inflate(R.layout.actionbar_bottom_iv, null);
//        rlp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        assert iv != null;
//        relativeLayout.addView(iv, rlp);
//
//        LinearLayout linearLayout = new LinearLayout(activity);
//        LinearLayout.LayoutParams llp;
//        linearLayout.setOrientation(LinearLayout.VERTICAL);
//        linearLayout.setClickable(true);
//
//        //添加actionbar
//        View actionBar = activity.getLayoutInflater().inflate(R.layout.action_bar, null);
//        llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtils.dip2px(50f));
//        assert actionBar != null;
//        linearLayout.addView(actionBar, llp);
//        View content = activity.getLayoutInflater().inflate(layoutResID, null);
//        llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//        assert content != null;
//        linearLayout.addView(content, llp);
//
//        rlp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//        relativeLayout.addView(linearLayout, rlp);
//
//        return relativeLayout;
//    }

// *************************************************************************
//  其它
// *************************************************************************

    /**
     * 设置TextView 的各个方向的图片
     *
     * @param textView TextView
     * @param location 位置 0-left, 1-top, 2-right, 3-bottom, 否则不设置
     * @param resId    资源ID
     */
    public static void setTextViewDrawable(Context c, TextView textView, int resId, int location) {
        setTextViewDrawable(textView, ResUtil.getDrawable(c, resId), location);
    }

    /**
     * 设置TextView 的各个方向的图片
     *
     * @param textView TextView
     * @param drawable 图片对象drawable
     * @param location 位置 0-left, 1-top, 2-right, 3-bottom，否则不设置
     */
    public static void setTextViewDrawable(final TextView textView, Drawable drawable, int location) {
        if (textView == null) return;
        if (location == 0) {
            textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        } else if (location == 1) {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
        } else if (location == 2) {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        } else if (location == 3) {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
        } else {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
    }


    public static View addFlowLabel(Context c, int layoutId, int tvId, String label, int drawableId) {
        final View view = createView(c, layoutId);
        TextView textView = view.findViewById(tvId);
        textView.setText(label);
        textView.setBackgroundResource(drawableId);
        return view;
    }

    public static View createView(Context context, int layoutID) {
        return LayoutInflater.from(context).inflate(layoutID, null);
    }
}
